﻿namespace FiftyOne.Foundation.Mobile.Configuration
{
    using System;
    using System.Configuration;
    using System.Reflection;

    public sealed class UrlCollection : ConfigurationElementCollection
    {
        public void Add(UrlElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            this.BaseAdd(element);
        }

        protected override void BaseAdd(ConfigurationElement element)
        {
            base.BaseAdd(element, false);
        }

        public void Clear()
        {
            base.BaseClear();
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new UrlElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((UrlElement) element).Url;
        }

        public int IndexOf(UrlElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            return base.BaseIndexOf(element);
        }

        public void Remove(UrlElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            if (base.BaseIndexOf(element) >= 0)
            {
                base.BaseRemove(element.Url);
            }
        }

        public void Remove(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }
            base.BaseRemove(url);
        }

        public void RemoveAt(int index)
        {
            if ((index < 0) || (index > (base.Count - 1)))
            {
                throw new ArgumentOutOfRangeException("index");
            }
            base.BaseRemoveAt(index);
        }

        public UrlElement this[string name]
        {
            get
            {
                return (UrlElement) base.BaseGet(name);
            }
        }

        public UrlElement this[int index]
        {
            get
            {
                return (UrlElement) base.BaseGet(index);
            }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }
    }
}

